import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:fate_blind_box/im/person_info.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../network/Api.dart';
import '../utils/assets.dart';
import 'package:fate_blind_box/routes/page_route_names.dart';
import '../utils/provider.dart';
import 'addgroupmember.dart';

class GroupAllPersonPage extends StatefulWidget {
  String groupId;

  GroupAllPersonPage(this.groupId, {Key? key}) : super(key: key);

  @override
  State<GroupAllPersonPage> createState() => _GroupAllPersonPageState();
}

class CusBehavior extends ScrollBehavior {
  @override
  Widget buildViewportChrome(
      BuildContext context, Widget child, AxisDirection axisDirection) {
    if (Platform.isAndroid || Platform.isFuchsia) return child;
    return buildViewportChrome(context, child, axisDirection);
  }
}

class _GroupAllPersonPageState extends State<GroupAllPersonPage> {
  late final List _listData = [
    {'user': '+'},
    // {'user': '-'},
  ];
  late String datas = "";
  var memberEaseGroup;

  @override
  void initState() {
    super.initState();

    _getGroupInfo();
  }

  // 获取群组信息
  _getGroupInfo() async {
    final easeName = await Provider.getUserEaseName();
    if (kDebugMode) {
      print('rx--环信用户id-------' + Provider.userEaseName);
      print('rx--环信用户id-------' + widget.groupId);
    }
    if (widget.groupId.isEmpty || Provider.userEaseName.isEmpty) return;
    final res = await Api.getInstance()
        .getGroupAllMembers(widget.groupId, Provider.userEaseName);
    var data = res['data'];
    if (data != null) {
      List list = data;
      if (list.isNotEmpty == true) {
        setState(() {
          _listData.clear();
          _listData.addAll(list);
          _listData.add({'user': '+'});
          // _listData.add({'user': '-'});
        });
      }
    } else {
      if (kDebugMode) {
        print('------获取群成员异常---------------' + res['msg']);
      }
    }
  }

  Widget memberItem(item) {
    List<dynamic> userInfo;
    String uId;
    String uFace = '';
    String nickName;
    print('-------------------' + item.toString());
    if (item['user'] == "+" || item['user'] == '-') {
      return InkWell(
        child: SizedBox(
          width: 60,
          child: Image.asset(
            'images/${item['user']}.png',
            height: 48.0,
            width: 48.0,
          ),
        ),
        onTap: () {
          if (item['user'] == "+") {
            //拉人
            Navigator.of(context).push(MaterialPageRoute(
                builder: (builder) =>
                    AddGroupMember(item['easeuserId'].toString(), "", '2')));
          }
        },
      );
    }
    return SizedBox(
      width: 60,
      child: InkWell(
        onTap: () {
          if (item['id'].toString().isEmpty) return;
          Get.to(PersonInfo(sig: item['id'].toString()),
              routeName: PageRouteNames.personInfoPage);
        },
        highlightColor: Colors.transparent,
        child: Column(
          children: <Widget>[
            ClipRRect(
              borderRadius: const BorderRadius.all(Radius.circular(5)),
              child: CachedNetworkImage(
                imageUrl:
                    Provider.fixAvatar(item['profilePhoto'].toString()),
                height: 50.0,
                width: 50.0,
                fit: BoxFit.cover,
              ),
            ),
            const SizedBox(height: 2),
            Container(
              alignment: Alignment.center,
              height: 20.0,
              width: 50,
              child: Text(
                item['nickName'].toString(),
                style: const TextStyle(
                  fontSize: 12.0,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: getCommonAppBar('聊天成员(${_listData.length - 1})',
            color: Colors.black),
        body: SingleChildScrollView(
          child: ScrollConfiguration(
            behavior: CusBehavior(),
            child: Container(
              color: Colors.white,
              padding: const EdgeInsets.only(top: 10.0, bottom: 50),
              child: Wrap(
                runSpacing: 20.0,
                spacing: 10,
                children: _listData.map(memberItem).toList(),
              ),
            ),
          ),
        ));
  }
}
